//
//  DataBaseSingleton.m
//  豆瓣终极版
//
//  Created by dllo on 15/12/19.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "DataBaseSingleton.h"
#import <sqlite3.h>
#import "Movie.h"
@implementation DataBaseSingleton

+ (instancetype)shareDataBase{
    static DataBaseSingleton *dataBase = nil;
    if (dataBase == nil) {
        dataBase = [[DataBaseSingleton alloc] init];
        
    }
    return dataBase;
}

// 创建一个数据库的对象
static sqlite3 *db = nil;

// 1.创建一个方法打开数据库
- (void)openDb{
    if (db != nil) {
        NSLog(@"数据库已经打开");
        return;
    }
    // 拼一个数据库的文件路径
    NSString *doucument = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *filePath = [doucument stringByAppendingPathComponent:@"Movie.sqlite"];
    NSLog(@"filePath = %@", filePath);
    // 打开数据库
    BOOL result = sqlite3_open(filePath.UTF8String, &db);
    if (result == SQLITE_OK) {
        NSLog(@"打开成功");
    } else {
        NSLog(@"打开失败");
    }
    
}

// 创建电影收藏表
- (void)createMovieTable{
    NSString *sql = @"CREATE TABLE IF NOT EXISTS movietable(number INTEGER PRIMARY KEY AUTOINCREMENT, movieId TEXT, movieName TEXT , pic_url TEXT)";
    BOOL result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"用户表创建成功");
    } else {
        NSLog(@"用户表创建失败");
    }
    
    
}

// 插入电影信息
- (void)insertMovieInToTable:(Movie *)movie{
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO movietable(movieId, movieName , pic_url) VALUES('%@', '%@', '%@')", movie.movieId, movie.movieName, movie.pic_url];
    BOOL result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"插入成功");
    } else {
        NSLog(@"插入失败");
    }
    
}

// 删除电影收藏表
- (void)deleteMovieTable:(NSString *)movieName{
    NSString *sql = [NSString stringWithFormat:@"DELETE FROM movietable WHERE movieName = '%@'", movieName];
    BOOL result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    } else {
        NSLog(@"删除失败");
    }
}

// 查询电影收藏表
- (NSMutableArray *)selectMovieTable{
    
    NSMutableArray *array = [NSMutableArray array];
    //    NSString *sql = [NSString stringWithFormat:@"SELECT * FROM activitytable WHERE title = '%@'", act.title];
    NSString *sql = @"SELECT * FROM movietable ";
    // 创建数据库跟随指针,主要用于遍历查询表中每一行上的数据
    sqlite3_stmt *stmt = nil;
    BOOL result = sqlite3_prepare_v2(db, sql.UTF8String, -1, &stmt, nil);
    if (result == SQLITE_OK) {
        NSLog(@"查询准备成功");
    } else {
        NSLog(@"查询准备失败");
        return  nil;
    }
    while (sqlite3_step(stmt) == SQLITE_ROW) {
        const unsigned char *charMovieId = sqlite3_column_text(stmt, 1);
        const unsigned char *charMovieName = sqlite3_column_text(stmt, 2);
        const unsigned char *charPic = sqlite3_column_text(stmt, 3);
        
        NSString *movieId = [NSString stringWithUTF8String:(const char *) charMovieId];
        NSString *movieName = [NSString stringWithUTF8String:(const char *) charMovieName];
        NSString *pic = [NSString stringWithUTF8String:(const char *) charPic];
        
        Movie *movie = [[Movie alloc] init];
        movie.movieId = movieId;
        movie.movieName = movieName;
        movie.pic_url = pic;
        [array addObject:movie];
        
    }
    return array;
}

@end
